package com.banciyuan.bcywebview.biz.main.mineinfo.sina;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.SinaUser;

/**
 * Created by squall on 2016/6/15.
 */
public class SinaFriendAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<SinaUser> slist = new ArrayList<>();

    public SinaFriendAdapter(Context mContext, List<SinaUser> slist) {
        this.mContext = mContext;
        this.slist = slist;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return slist.size() == 0 ? 0 : 1;
    }

    @Override
    public int getCount() {
        return slist.size() == 0 ? 1 : slist.size();
    }

    @Override
    public Object getItem(int position) {
        return slist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        private TextView name_tv;
        private TextView sina_tv;
        private ImageView sina_iv;
        private TextView focus_tv;

        public ViewHolder(View convertView) {
            sina_iv = (ImageView) convertView.findViewById(R.id.head_iv);
            focus_tv = (TextView) convertView.findViewById(R.id.foucs_tv);
            name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            sina_tv = (TextView) convertView.findViewById(R.id.sinaname_tv);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                convertView = inflater.inflate(R.layout.no_sina_layout, null);
            } else {
                convertView = inflater.inflate(R.layout.sina_user_item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }

        } else {
            if (getItemViewType(position) == 0) {
                convertView = inflater.inflate(R.layout.no_sina_layout, null);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
        }
        if (getItemViewType(position) == 1) {
            final SinaUser sinaUser = slist.get(position);
            final ViewHolder finalViewHolder = viewHolder;
            if (!TextUtils.isEmpty(sinaUser.getUname()))
                viewHolder.name_tv.setText(sinaUser.getUname());
            if (!TextUtils.isEmpty(sinaUser.getSina_name()))
                viewHolder.sina_tv.setText(sinaUser.getSina_name());
            if (!TextUtils.isEmpty(sinaUser.getAvatar()))
                ImageLoader.getInstance().displayImage(sinaUser.getAvatar(), viewHolder.sina_iv, ImageOptions.optionHead);
            if (sinaUser.getFollowstate().equals("unfollow")) {
                viewHolder.focus_tv.setVisibility(View.VISIBLE);
                viewHolder.focus_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TimelineLikeHelper.doFollow(mContext, "dofollow", sinaUser.getUid(), new TimelineLikeHelper.FollowSuccess() {
                            @Override
                            public void onFollowSuccess(String response) {
                                MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                                finalViewHolder.focus_tv.setVisibility(View.GONE);
                            }
                        });
                    }
                });
            } else {
                viewHolder.focus_tv.setVisibility(View.GONE);
            }
            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, sinaUser.getUid());
                }
            };
            ((View) (viewHolder.sina_iv.getParent())).setOnClickListener(onClickListener);

        }

        return convertView;
    }
}
